package com.nuckyding.controler;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.rowset.serial.SerialBlob;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.WebApplicationContext;

import com.nuckyding.pojo.AlarmQuery;
import com.nuckyding.pojo.ChickTime;
import com.nuckyding.service.RollBookService;

import org.apache.catalina.connector.Request;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
@Controller
public class RollBookController {
	private Integer count = 100;
	
	@Resource
	private RollBookService service;
	@RequestMapping("/rollbooklist.action")
	public String list(String beginTime, String endTime,Model model,HttpServletResponse response,HttpServletRequest request)throws Exception{
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//小写的mm表示的是分钟  
		Date be=sdf.parse(beginTime);  
		Date end=sdf.parse(endTime);  
		ChickTime time = new ChickTime(be,end);
		List<AlarmQuery>list = 	  service.rollBook(time);
		pic(list, request);
	//	out(list);
		StringBuffer stringBuffer = new StringBuffer();
		if(list!=null &&list.size()>0){
			stringBuffer.append("应到人数"+count+"---");
			stringBuffer.append("实际捕捉人数"+list.size()+"---");
			if(count ==list.size()){
				stringBuffer.append("数据显示人员到齐");
			}else
				if(count >list.size()){
					stringBuffer.append("查询数据缺少"+(count -list.size())+"人,请核对");
				}
				else
					if(count <list.size()){
						stringBuffer.append("查询数据多出"+(list.size()-count )+"人,请核对");
					}
		}
		model.addAttribute("list", list);
		model.addAttribute("S", stringBuffer.toString());
		model.addAttribute("beginTime", time.getBeginTime());
		model.addAttribute("endTime", time.getEndTime());
		return "center";
		}
	
	 private void pic(List<AlarmQuery> list,HttpServletRequest request ) throws Exception {
		// TODO Auto-generated method stub
		 if(list !=null && list.size()>0){
			 for (int j = 0; j < list.size(); j++) {
				 String realPath = "E:/pm/ssmtext/WebContent/pics123";
				 System.out.println(realPath);
				 
				 byte[] photo = list.get(j).getPhoto();
				 byte[] facePhoto = list.get(j).getFacePhoto();
				 File file = new File(realPath,j+"photo.jpg");
				 File file2 = new File(realPath,j+"facePhoto.jpg");
				 FileOutputStream fileOutputStream = new FileOutputStream(file);
				 FileOutputStream fileOutputStream2 = new FileOutputStream(file2);
				 fileOutputStream.write(photo);
				 fileOutputStream2.write(facePhoto);
				 fileOutputStream.close();
				 fileOutputStream2.close();
			}
		 }
		
	 }

	/*@SuppressWarnings("deprecation")
	public  void out(List<AlarmQuery>list) throws Exception  
	    {  
	        // 第一步，创建一个webbook，对应一个Excel文件  
	        HSSFWorkbook wb = new HSSFWorkbook();  
	        // 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet  
	        HSSFSheet sheet = wb.createSheet("表一");  
	        // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short  
	        HSSFRow row = sheet.createRow((int) 0);  
	        // 第四步，创建单元格，并设置值表头 设置表头居中  
	        HSSFCellStyle style = wb.createCellStyle();  
	        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式  
	        HSSFCell cell = row.createCell((short) 0);  
	        cell.setCellValue("时间");  
	        cell.setCellStyle(style);  
	        cell = row.createCell((short) 1);  
	        cell.setCellValue("姓名");  
	        cell.setCellStyle(style);  
	        cell = row.createCell((short) 2);  
	        cell.setCellValue("捕捉照片");  
	        cell.setCellStyle(style);  
	        cell = row.createCell((short) 3);  
	        cell.setCellValue("匹配照片");  
	        cell.setCellStyle(style);  
	        cell = row.createCell((short) 4);  
	        cell.setCellValue("匹配得分");  
	        cell.setCellStyle(style);  
	  
	        // 第五步，写入实体数据 实际应用中这些数据从数据库得到，  
	        //List list = CreateSimpleExcelToDisk.getStudent();  
	  
	        for (int i = 0; i < list.size(); i++)  
	        {  
	            row = sheet.createRow((int) i + 1);  
	            AlarmQuery aq =  list.get(i);  
	            // 第四步，创建单元格，并设置值  
	            row.createCell((short) 0).setCellValue(aq.getAlarmTime());  
	            row.createCell((short) 1).setCellValue(aq.getName()); 
	            
	            
//	            row.createCell((short) 2).setCellValue(aq.getFacePhoto().toString());  
//	            row.createCell((short) 3).setCellValue(aq.getPhoto().toString());  
	            row.createCell((short) 4).setCellValue(aq.getScore());  
	            
	            FileOutputStream fileOut = null;     
	            FileOutputStream fileOut2 = null;     
	            BufferedImage bufferImg = null;     
	            BufferedImage bufferImg2 = null;     
	           //先把读进来的图片放到一个ByteArrayOutputStream中，以便产生ByteArray    
	           try {  
	               ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();     
	               bufferImg = ImageIO.read(new File("pics123/"+i+"facePhoto.jpg"));     
	               bufferImg2= ImageIO.read(new File("pics123/"+i+"photo.jpg"));     
	               ImageIO.write(bufferImg, "jpg", byteArrayOut);  
	               //画图的顶级管理器，一个sheet只能获取一个（一定要注意这点）  
	               HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();     
	               //anchor主要用于设置图片的属性  
	               HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 255, 255,(short) 1, 1, (short) 5, 8);     
	               anchor.setAnchorType(3);     
	               //插入图片    
	               patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));   
	               fileOut = new FileOutputStream("D:/测试Excel.xls");     
	               // 写入excel文件     
	                wb.write(fileOut);     
	        
	        // 第六步，将文件存到指定位置  
	        try  
	        {  
	            FileOutputStream fout = new FileOutputStream("点名册.xls");  
	            wb.write(fout);  
	            fout.close();  
	        }  
	        catch (Exception e)  
	        {  
	            e.printStackTrace();  
	        }  
	    }  */
}
